package com.ilop.sthome.mvp.model;

import android.os.Handler;

import com.aliyun.iot.aep.sdk.apiclient.IoTAPIClient;
import com.aliyun.iot.aep.sdk.apiclient.IoTAPIClientFactory;
import com.aliyun.iot.aep.sdk.apiclient.callback.IoTCallback;
import com.aliyun.iot.aep.sdk.apiclient.callback.IoTResponse;
import com.aliyun.iot.aep.sdk.apiclient.emuns.Scheme;
import com.aliyun.iot.aep.sdk.apiclient.request.IoTRequest;
import com.aliyun.iot.aep.sdk.apiclient.request.IoTRequestBuilder;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.mvp.model.common.ShareModelImpl;
import com.example.common.mvp.onModelCallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author skygge
 * @Date on 2020-02-19.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备分享
 */
public class ShareModel implements ShareModelImpl {

    private Handler mHandler;

    public ShareModel(){

    }

    public ShareModel(Handler mHandler) {
        this.mHandler = mHandler;
    }

    @Override
    public void registerCheck(String type, String address, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        maps.put(type, address);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/user/account/regcheck")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.0")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void getShareNoticeList(int pageNo, int pageSize, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("pageNo", pageNo);
        maps.put("pageSize", pageSize);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/getShareNoticeList")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.2")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void getBindingByDevice(String iotId, int pageNo, int pageSize, int owned, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("iotId", iotId);
        maps.put("pageNo", pageNo);
        maps.put("pageSize", pageSize);
        maps.put("owned", owned);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/listBindingByDev")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.2")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void shareDevicesAndScenes(String iotId, String accountAttr, String accountAttrType, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(iotId);
        maps.put("iotIdList", list);
        maps.put("accountAttr", accountAttr);
        maps.put("accountAttrType", accountAttrType);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/shareDevicesAndScenes")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.7")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void cancelShare(String recordId, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(recordId);
        maps.put("recordIdList",list);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/cancelShare")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.2")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void confirmShare(String recordId, int agree, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(recordId);
        maps.put("recordIdList", list);
        maps.put("agree", agree);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/confirmShare")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.2")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void unbindByManager(String targetIdentityId, String iotId, onModelCallBack callBack) {
        Map<String, Object> maps = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(iotId);
        maps.put("targetIdentityId", targetIdentityId);
        maps.put("iotIdList", list);
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/unbindByManager")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.2")
                .setAuthType("iotAuth")
                .setParams(maps);
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    @Override
    public void clearShareNoticeList(onModelCallBack callBack) {
        IoTRequestBuilder builder = new IoTRequestBuilder()
                .setPath("/uc/clearShareNoticeList")
                .setScheme(Scheme.HTTPS)
                .setApiVersion("1.0.2")
                .setAuthType("iotAuth");
        IoTRequest request = builder.build();
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest ioTRequest, Exception e) {
                callBack.onFailure(e);
            }

            @Override
            public void onResponse(IoTRequest ioTRequest, IoTResponse response) {
                if (response.getCode() == 401){
                    onLoginOut();
                    return;
                }
                callBack.onResponse(response);
            }
        });
    }

    private void onLoginOut(){
        if (mHandler != null) {
            mHandler.post(() -> LiveDataBus.get().with(CommonId.LOGIN_OUT).setValue(0));
        }
    }
}
